package com.itheima.springboot02_demo01.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.jboss.C3P0PooledDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import javax.sql.XADataSource;

@Configuration
public class DataSourceConfiguration {

    @Bean
    @ConditionalOnMissingBean({ DataSource.class })
    public DataSource druidDataSource(DataSourceProperties properties){
        System.out.println("我正在创建druid连接池");
        DruidDataSource dataSource = new DruidDataSource();
        System.out.println("获取数据库的配置");
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        dataSource.setUrl(properties.getUrl());
        dataSource.setDriverClassName(properties.getDriverClassName());
        return dataSource;
    }

    @Bean
    @ConditionalOnMissingBean({ DataSource.class })
    public DataSource c3p0DataSource(){
        System.out.println("我正在创建c3p0连接池");
        ComboPooledDataSource c3P0PooledDataSource = new ComboPooledDataSource();
        return (DataSource) c3P0PooledDataSource;
    }

}
